package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.OccupancyBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.OccupancyMapper;
import com.java2207.group1.pojo.Hospitalized;
import com.java2207.group1.pojo.Income;
import com.java2207.group1.pojo.Occupancy;
import com.java2207.group1.pojo.OldMan;
import com.java2207.group1.service.FinancialManagementService;
import com.java2207.group1.service.HospitalizedService;
import com.java2207.group1.service.OccupancyService;
import com.java2207.group1.service.OldManService;
import com.java2207.group1.vo.occupancy.BerVo;
import com.java2207.group1.vo.occupancy.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 15319 2022/7/27 14:20
 */
@Service("occupancyService")
public class OccupancyServiceImpl implements OccupancyService {
    @Autowired
    private OccupancyMapper occupancyMapper;


    @Override
    public LayuiPageVo<Occupancy> occupancyPage(Map<String, Object> params) {
        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page,limit);

        List<Occupancy> occupancyPage = occupancyMapper.occupancyPage(params);
        PageInfo<Occupancy> occupancyPageInfo = new PageInfo<>(occupancyPage);

        LayuiPageVo<Occupancy> layuiPageVo = new LayuiPageVo<>();
        layuiPageVo.setCode(0);
        layuiPageVo.setMsg("入院列表");
        layuiPageVo.setCount(occupancyPageInfo.getTotal());
        layuiPageVo.setData(occupancyPageInfo.getList());

        return layuiPageVo;
    }

    @Override
    public int occupancyUpdate(Occupancy occupancy) {
        return occupancyMapper.occupancyUpdate(occupancy);
    }

    @Override
    public int occupancyDelete(Integer id) {
        return occupancyMapper.occupancyDelete(id);
    }


    @Autowired
    private FinancialManagementService financialManagementService;
    @Autowired
    private HospitalizedService hospitalizedService;

    @Autowired
    private OldManService oldManService;
    @Override
    public int occupancyAdd(Occupancy occupancy) {
        OldMan oldMan = new OldMan();
        oldMan.setId(occupancy.getId());
        oldMan.setName(occupancy.getName());
        oldMan.setAge(occupancy.getAge());
        oldMan.setSex(occupancy.getSex());
        oldMan.setGuardian(occupancy.getPhone());
        oldMan.setGuardian_tel(occupancy.getPhone());
        oldMan.setAnnihdate(occupancy.getAnnihdate());
        oldMan.setPic(occupancy.getPic());
        oldManService.addOldMan(oldMan);



        Hospitalized hospitalized = new Hospitalized();
        hospitalized.setId(occupancy.getId());
        hospitalized.setName(occupancy.getName());
        hospitalized.setAge(occupancy.getAge());
        hospitalized.setSex(occupancy.getSex());
        hospitalized.setGuardian(occupancy.getGuardian());
        hospitalizedService.hospitalizedAdd(hospitalized);

        Income income = new Income(occupancy.getId(), occupancy.getName(), occupancy.getPhone(), Integer.valueOf(occupancy.getDeposit()), occupancy.getAnnihdate());
        int i = financialManagementService.addIncome(income);

        return occupancyMapper.occupancyAdd(occupancy);
    }

    @Override
    public int occupancyDeleteByIds(String ids) {
        String[] id = ids.split(",");
        return occupancyMapper.occupancyDeleteByIds(id);
    }

    @Override
    public List<BerVo> setBarDate() {
        return occupancyMapper.setBarDate();
    }

    @Override
    public List<PieVo> setPieDate() {
        return occupancyMapper.setPieDate();
    }

    @Override
    public List<OccupancyBo> occupancyExcelPage(Map<String, Object> params) {
        List<Occupancy> occupancyPage = occupancyMapper.occupancyPage(params);
        List<OccupancyBo> collect = occupancyPage.stream().map(occupancy -> {
            OccupancyBo occupancyBo = new OccupancyBo();
            occupancyBo.setId(occupancy.getId());
            occupancyBo.setName(occupancy.getName());
            occupancyBo.setAge(occupancy.getAge());
            occupancyBo.setSex(occupancy.getSex());
            occupancyBo.setGuardian(occupancy.getGuardian());
            occupancyBo.setPhone(occupancy.getPhone());
            occupancyBo.setHealth(occupancy.getHealth());
            occupancyBo.setAnnihdate(occupancy.getAnnihdate());
            occupancyBo.setDeposit(occupancy.getDeposit());
            return occupancyBo;
        }).collect(Collectors.toList());

        return collect;

    }
}
